package com.hcx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hcx.reggie.pojo.ShoppingCart;
import com.hcx.reggie.service.ShoppingCartService;
import com.hcx.reggie.utils.BaseContext;
import com.hcx.reggie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@CrossOrigin
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /***
     * 获取购物车的列表
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
       //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        //调用service层的获取购物车列表方法
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> cartList = shoppingCartService.list(wrapper);
        //返回查询到的结果
        return R.success(cartList);
    }
    /**
     * 添加购物车
     * @param cart
     * @return
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart cart) {
        //设置购物车的userId
        cart.setUserId(BaseContext.getCurrentId());
        Long dishId = cart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, cart.getUserId());
        if(dishId != null) {
            //查询当前的菜品是否已经在购物车中
            wrapper.eq(ShoppingCart::getDishId, cart.getDishId());
        }else{
            //根据setMealId和userId查询购物车中是否已经有该套餐
            wrapper.eq(ShoppingCart::getSetmealId, cart.getSetmealId());
        }
        //调用service层的添加购物车方法
        ShoppingCart shoppingCart = shoppingCartService.getOne(wrapper);
        if (shoppingCart != null) {
            //如果已经在购物车中，则更新数量
            cart.setNumber(shoppingCart.getNumber() + 1);
            cart.setId(shoppingCart.getId());
            shoppingCartService.updateById(cart);
        }else{
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(cart);
        }
        //返回成功信息
       return R.success(cart);
    }
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart cart) {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        wrapper.eq(ShoppingCart::getDishId, cart.getDishId());
        ShoppingCart shoppingCart = shoppingCartService.getOne(wrapper);
        if (shoppingCart != null) {
            if (shoppingCart.getNumber() > 1) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartService.updateById(shoppingCart);
            } else {
                shoppingCartService.removeById(shoppingCart.getId());
            }
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("clean")
    public R<String> clean() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(wrapper);
        return R.success("清空成功");
    }

}
